package com.duing.util;

import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

public class HttpClientUtil {

    public static String serverUrl = "https://www.qidian.com/chapter/1044482577/838665575/";

    public static void main(String[] args) {;
        String result = doGet(serverUrl);
        System.out.println("???");
    }
    public static String doGet(String str) {
        CloseableHttpClient  client = null;
        CloseableHttpResponse response = null;
        String result = null;
        try {
            client = HttpClients.createDefault();
            HttpGet get = new HttpGet(str);

            RequestConfig requestConfig = RequestConfig.custom()
                    .setConnectTimeout(5000)
                    .setConnectionRequestTimeout(5000)
                    .setSocketTimeout(5000)
                    .build();

            get.setConfig(requestConfig);
            response = client.execute(get);
            HttpEntity entity = response.getEntity();
            result = EntityUtils.toString(entity);
        }catch (Exception e){
            System.out.println("error" + e.getMessage());
        }finally {
            try {
                if(client != null){
                    client.close();
                }
                if(response != null){
                    response.close();
                }
            }catch (Exception e){
                System.out.println("finally error" + e.getMessage());
            }
        }
        return result;
    }
}
